import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hongsheng/app/SPClassApplicaion.dart';
import 'package:hongsheng/pages/expert/SPClassExpertLeaderboardPage.dart';
import 'package:hongsheng/pages/home/SPClassHomePage.dart';
import 'package:hongsheng/utils/SPClassCommonMethods.dart';
import 'package:hongsheng/utils/SPClassImageUtil.dart';
import 'package:hongsheng/utils/SPClassNavigatorUtils.dart';
import 'package:hongsheng/api/SPClassApiManager.dart';
import 'package:hongsheng/pages/anylise/SPClassSearchExpertPage.dart';
import 'package:hongsheng/widgets/SPClassSwitchTabBar.dart';
import 'package:hongsheng/widgets/SPClassToolBar.dart';
import 'package:hongsheng/utils/SPClassStringUtils.dart';


class SPClassExpertHomePage extends StatefulWidget{
  SPClassExpertHomePageState ?spProState;

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return  spProState=SPClassExpertHomePageState();
  }

}

class SPClassExpertHomePageState extends State<SPClassExpertHomePage> with TickerProviderStateMixin ,AutomaticKeepAliveClientMixin{
  // var titles=["关注","榜单","专家"];
  var titles=["足球","篮球",];
  var spProTabTitle=["关注专家的方案","榜单"];
  List spProTabExpertKeys=["is_zq_expert","is_lq_expert"];

  List<SPClassExpertLeaderboardPage>? views;
  static int index=1;
  PageController ?spProPageController;
  SPClassSwitchTabBarState? spProSwitchTabBarController;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    spProPageController=PageController(initialPage: index);
    //views=[/*FollowHomePage(),*/SPClassExpertLeaderboardPage()/*,ExpertListPage()*/];
    views=[SPClassExpertLeaderboardPage()];
    /*if(index==1){
      ApiManager.getInstance().logAppEvent(spProEventName: "view_expert_ranking",);
    }*/
    SPClassApplicaion.spProEventBus.on<String>().listen((event) {
      if(event.startsWith("scheme:refresh")){
        if(SPClassHomePageState.spProHomeMatchTypeKey=='关注'){return;}
        spProSwitchTabBarController?.spFunStartAnmat(spProTabExpertKeys.indexOf(SPClassHomePageState.spProHomeMatchTypeKey));
      }

    });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    super.build(context);
    return Scaffold(
      appBar:AppBar(
          centerTitle: true,
          backgroundColor: Colors.white,
          elevation: 0,
          brightness: Brightness.light,
          title:  SPClassSwitchTabBar(
            spProTabTitles:titles,
            index:SPClassHomePageState.spProHomeMatchTypeKey=='关注'?0:spProTabExpertKeys.indexOf(SPClassHomePageState.spProHomeMatchTypeKey),
            fontSize: sp(16),
            width: width(67*titles.length),
            height: width(29),
            spProGetState: (state){
              spProSwitchTabBarController=state;
            },
            spProTabChanged: (index){
              SPClassHomePageState.spProHomeMatchTypeKey=spProTabExpertKeys[index];
              views![0].spProState?.onRefresh();
            },
          ),
          actions: <Widget>[
            Container(
              margin: EdgeInsets.only(right: width(20)),
              child: GestureDetector(
                child: Image.asset(
                  SPClassImageUtil.spFunGetImagePath("ic_search"),
                  width: width(16),
                  fit: BoxFit.fitWidth,
                  color: Color(0xFF333333),
                ),
                onTap: (){
                  SPClassNavigatorUtils.spFunPushRoute(context, SPClassSearchExpertPage());
                },
              ),
            ),
          ]

      ),
      body:Container(
        color: Color(0xFFF1F1F1),
        child: Column(
          children: <Widget>[
            Expanded(
              child: PageView(
                physics: NeverScrollableScrollPhysics(),
                controller: spProPageController,
                children: views!,
              ),
            )
          ],
        ),
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

/* void tapTopItem(index){
    spProPageController.jumpToPage(index);
    setState(() {
      ExpertHomePageState.index=index;
    });
    if(index==1){
      ApiManager.getInstance().logAppEvent(spProEventName: "view_expert_ranking",);
    }
    if(index==2){
      ApiManager.getInstance().logAppEvent(spProEventName: "view_expert_list",);
    }
  }*/

}